Skip to content

Conversation

@phernandez
Copy link
Member

Fixes #481

Problem

The alembic/env.py module was unconditionally setting BASIC_MEMORY_ENV='test' at import time, which caused config.is_test_env to always return True. This prevented the MCP server from starting the file watch service, breaking file sync for external changes.

Solution

Only set the env var when PYTEST_CURRENT_TEST is set, indicating an actual pytest run. This preserves test behavior while enabling watch service in production.

Testing

  • Tests remain safe because PYTEST_CURRENT_TEST is always set during pytest runs
  • tests/__init__.py also sets the env var for additional safety
  • config.is_test_env checks PYTEST_CURRENT_TEST directly as a third condition

———
Generated with Claude Code

The alembic/env.py module was unconditionally setting BASIC_MEMORY_ENV='test'
at import time, which caused config.is_test_env to always return True. This
prevented the MCP server from starting the file watch service, breaking file
sync for external changes.

Fix: Only set the env var when PYTEST_CURRENT_TEST is set, indicating an
actual pytest run. This preserves test behavior while enabling watch service
in production.

Fixes #481

Co-authored-by: Paul Hernandez <[email protected]>
Signed-off-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com>
@github-actions
Copy link
Contributor

github-actions bot commented Dec 29, 2025

Claude finished @phernandez's task in 1m 44s —— View job


PR Review Complete ✅

I've reviewed PR #482 against the Basic Memory team checklist. This is a well-executed bug fix with excellent literate programming style.


Code Quality & Standards ✅

  • Follows Basic Memory's coding conventions - Excellent adherence to CLAUDE.md literate programming style
  • Python 3.12+ type annotations - Not applicable (no new functions)
  • 100-character line length - All lines comply
  • No commented-out code - Clean implementation
  • Decision Point Comments - Outstanding! The change includes a perfect example of the required comment format:
    # Trigger: only set test env when actually running under pytest
    # Why: alembic/env.py is imported during normal operations (MCP server startup, migrations)
    #      but we only want test behavior during actual test runs
    # Outcome: prevents is_test_env from returning True in production, enabling watch service

Testing & Documentation ✅

  • Test safety verified - Multiple layers of test protection:
    1. PYTEST_CURRENT_TEST is automatically set by pytest (lines 24-29 in alembic/env.py)
    2. tests/__init__.py also sets BASIC_MEMORY_ENV='test' as backup (line 4)
    3. config.is_test_env checks PYTEST_CURRENT_TEST directly as third condition (config.py:249)
  • Edge cases covered - The triple-check approach (env var + pytest detection + tests/init.py) is robust
  • [✓] Documentation - PR description clearly explains the problem and solution
  • [⚠️] Test coverage gap - See recommendation below

Recommendation: Consider adding a test that verifies config.is_test_env returns False when alembic.env is imported outside pytest context. This would ensure the fix works as intended.


Basic Memory Architecture ✅

  • Preserves local-first architecture - Fix enables the file watch service in production
  • No database changes - No Alembic migrations needed
  • Minimal diff - Perfect example of "smallest change that satisfies the request" (CLAUDE.md line 64)
  • Fixes root cause - Addresses the import-time side effect that broke MCP server startup

Security & Performance ✅

  • No security concerns - Only changes when env var is set
  • No performance impact - Single env var check at import time
  • Proper error handling - Uses defensive check (is not None)

Compatibility ✅

  • Cross-platform - os.getenv() works on all platforms
  • No emojis/unicode in code - Clean ASCII only

Context Verification

I verified the fix is sound by checking:

  1. src/basic_memory/config.py:236-250 - is_test_env property checks PYTEST_CURRENT_TEST as third condition
  2. src/basic_memory/mcp/server.py:43-44 - MCP server skips file sync when is_test_env is True
  3. tests/init.py:4 - Sets BASIC_MEMORY_ENV='test' as additional safety layer
  4. Issue [BUG] Broken file sync when using mcp #481 - This fixes the exact problem where is_test_env was always True, preventing watch service

Summary

Approve with minor recommendation. This PR demonstrates excellent code quality:

  • ✅ Minimal, surgical fix
  • ✅ Outstanding literate programming comments
  • ✅ Triple-layer test safety
  • ✅ Solves the root cause

The only suggestion is to add a test that verifies config.is_test_env behaves correctly when alembic.env is imported in non-test contexts, but this is not blocking.

Great work on following the literate programming guidelines! The decision point comment is a textbook example of what CLAUDE.md requests.


@phernandez phernandez merged commit 98fbd60 into main Dec 29, 2025
20 checks passed
@phernandez phernandez deleted the claude/issue-481-20251229-1510 branch December 29, 2025 16:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG] Broken file sync when using mcp

2 participants